package main

const (
	brokerUrl  = "192.168.0.248"
	brokerPort = 1883
	username   = "admin"
	password   = "public"

	commandReqTopic  = "/edgex/command/device/modbus/req"
	commandRespTopic = "/edgex/command/device/modbus/resp"

	settingTopic      = "/edgex/events/device/modbus/setting"
	realtimeTopic     = "/edgex/events/device/modbus/realtime"
	brokensTopic      = "/edgex/events/device/modbus/spindles/brokens"
	weaksTopic        = "/edgex/events/device/modbus/spindles/weaks"
	noocupationsTopic = "/edgex/events/device/modbus/spindles/noocupations"
)

type RawRealtime struct {

	// 帧协议头 - 公共字节
	FrameIdentity uint16 `json:"frame_identity"` // 帧协议头标识  0xF5F5
	DataLen       uint16 `json:"data_len"`       // 数据部分长度

	Status      uint16 `json:"status"`
	Front_speed uint16 `json:"front_speed"`
	Avg_speed   uint16 `json:"avg_speed"`

	Spin_len   uint64 `json:"spin_len"`
	Theory_len uint64 `json:"theory_len"`
	Actual_len uint64 `json:"actual_len"`

	BrokensL_num      uint16 `json:"brokens_l_num"`
	NoocupationsL_num uint16 `json:"noocupations_l_num"`
	WeaksL_num        uint16 `json:"weaks_l_num"`
	MalfunctionsL_num uint16 `json:"malfunctions_l_num"`

	BrokensR_num      uint16 `json:"brokens_r_num"`
	NoocupationsR_num uint16 `json:"noocupations_r_num"`
	WeaksR_num        uint16 `json:"weaks_r_num"`
	MalfunctionsR_num uint16 `json:"malfunctions_r_num"`
}

// RawMcuSettings 结构体， PLC配置信息
// 每次重启后， MCU主动发自身配置参数
type RawMcuSettings struct {

	// 帧协议头 - 公共字节
	FrameIdentity uint16 `json:"frame_identity"` // 帧协议头标识  0xF5F5
	DataLen       uint16 `json:"data_len"`       // 数据部分长度
	TransactionID uint64 `json:"transaction_id"`

	Standard_twist       uint16 `json:"standard_twist"`       // 标准捻度
	Standard_front_speed uint16 `json:"standard_front_speed"` // 启动 罗拉转速， 也是 启动条件
	Spindle_mode         uint16 `json:"spindle_mode"`         // 锭号模式
	Spindle_count        uint16 `json:"spindle_count"`        // 总锭数

	Boot_duration    uint16 `json:"boot_duration"`    // 启动延时
	FRoller_Diameter uint16 `json:"froller_diameter"` // 前罗拉 直径
	FRoller_dividing uint16 `json:"froller_dividing"` // 前罗拉 分频

	Theory_Spinlen uint16 `json:"theory_spinlen"` // 设定 纺纱长度

	Boot_delay uint16 `json:"boot_delay"` // 落纱延时启动

	Roving_cut      uint16 `json:"roving_cut"`      // 粗纱开关
	Cyclic_Cut      uint16 `json:"cyclic_cut"`      // 循环切纱
	Cyclic_Cut_time uint16 `json:"cyclic_cut_time"` // 循环切纱时间
	Max_cuts        uint16 `json:"max_cuts"`        // 最大铲纱数

	Max_stop_feed uint16 `json:"max_stop_feed"` // 最大停喂数
	Min_stop_feed uint16 `json:"min_stop_feed"` // 最小停喂数
	Boot_speed    uint16 `json:"boot_speed"`    // 启动锭速

	// broken threshold speed & broken threshold duration
	Broken_threshold_speed    uint16 `json:"broken_threshold_speed"`    // 断头判断锭速
	Broken_threshold_duration uint16 `json:"broken_threshold_duration"` // 断头判断时间
	Broken_threshold_ratio    uint16 `json:"broken_threshold_ratio"`    // 断头锭速比例
	Fix_threshold_speed       uint16 `json:"fix_threshold_speed"`       // 复纺判断锭速
	Fix_threshold_duration    uint16 `json:"fix_threshold_duration"`    // 复纺判断时间
	Fix_delay                 uint16 `json:"fix_delay"`                 // 复纺生头延时
	Weak_speed_ratio          uint16 `json:"weak_speed_ratio"`          // 弱捻判断锭速比率
	Weak_threshold_duration   uint16 `json:"weak_threshold_duration"`   // 弱捻判断时间
	Nooc_threshold_duration   uint16 `json:"nooc_threshold_duration"`   // 空置判断时间
	Malfunc_Cond              uint16 `json:"malfunc_cond"`              // 故障判断条件

	Probe_cover        uint16 `json:"probe_cover"`         // 探头遮挡
	SignV              uint16 `json:"signv"`               // 信号电压
	PrecheckTime       uint16 `json:"precheck_time"`       // 断头预检设置
	LuminousIntensity  uint16 `json:"luminous_intensity"`  // 发光强度
	InductionIntensity uint16 `json:"induction_intensity"` // 感应强度

	Alloc_address  uint16 `json:"alloc_address"`  // 单锭学习按钮（第6位）, 线排学习按钮（第7位）
	Spin_Dev_count uint16 `json:"spin_dev_count"` // 单锭设备个数
	Mannual_cut_l  uint16 `json:"mannual_cut_l"`  // 手动切纱低
	Mannual_cut_h  uint16 `json:"mannual_cut_h"`  // 手动切纱高
	Mannual_cut    uint16 `json:"mannual_cut"`    // 手动切纱开关
	//Save           uint16 `json:"save"`           // 保存按钮

	Cut_Dev_count uint16 `json:"cut_dev_count"` // 线排设备个数

	Spin_Raddress uint16 `json:"spin_raddress"` // 单锭学习R地址
	Spin_Laddress uint16 `json:"spin_laddress"` // 单锭学习L地址
	Cut_Raddress  uint16 `json:"cut_raddress"`  // 线排学习R地址
	Cut_Laddress  uint16 `json:"cut_laddress"`  // 线排学习L地址

	Ver_main uint16 `json:"ver_main"`
	Ver_sub  uint16 `json:"ver_sub"`
}

type Parameter struct {
	Param string `json:"param"`
	Value uint16 `json:"value"`
}

type ReqParametersJsonBody struct {
	TransactionID uint64      `json:"transaction_id"` // 以前端的 时间戳 为 ID
	Timestamp     uint64      `json:"timestamp"`      // 回应 时间戳
	SendCmd       uint64      `json:"send_cmd"`       // 回应 时间戳
	Parameters    []Parameter `json:"parameters"`
}

// 可配置的 机台配置参数
func (c *ModbusClient) InitialRegisters() {
	c.configurableModbusRegisters = map[string]uint16{
		"standard_front_speed": 8202,
		"spindle_count":        8106,
		"froller_dividing":     8100,
		"froller_diameter":     8101,

		"theory_spinlen": 8102,

		"boot_duration": 8200,
		"boot_delay":    8200,

		"roving_cut":      8023,
		"cyclic_cut":      8090,
		"cyclic_cut_time": 8091,
		"max_cuts":        8130,

		"max_stop_feed": 8203,
		"min_stop_feed": 8206,
		"boot_speed":    8224,

		"broken_threshold_speed":    8300,
		"broken_threshold_duration": 8301,
		"broken_threshold_ratio":    8308,
		"fix_threshold_speed":       8302,
		"fix_threshold_duration":    8303,
		"fix_delay":                 8304,
		"weak_speed_ratio":          8305,
		"weak_threshold_duration":   8306,
		"nooc_threshold_duration":   8309,
		"malfunc_cond":              8310,

		"signv":               8307,
		"precheck_time":       8376,
		"probe_cover":         8377,
		"induction_intensity": 8381,
		"luminous_intensity":  8382,

		// 单锭学习按钮（第6位）, 线排学习按钮（第7位）
		"alloc_address": 8500,

		"spin_dev_count": 8504,
		"mannual_cut_l":  8505,
		"mannual_cut_h":  8506,
		"mannual_cut":    8507,

		"cut_dev_count": 8513,

		"spin_raddress": 8600,
		"spin_laddress": 8601,
		"cut_raddress":  8602,
		"cut_laddress":  8603,

		"ver_main": 9001,
		"ver_sub":  9002,
	}
}
